package com.jzj.dao;

import com.jzj.entity.Shop;
import com.mysql.cj.protocol.Resultset;

import javax.xml.transform.Result;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class BaseDao {
    private static String Driver;
    private static String url;
    private static String username;
    private static String password;
    Connection connection=null;
    PreparedStatement preparedStatement=null;
    ResultSet resultSet=null;

    static {
        InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("com/jzj/dao/a.properties");
        Properties properties = new Properties();
        try {
            properties.load(is);
            Driver=properties.getProperty("Driver");
            url=properties.getProperty("url");
            username=properties.getProperty("username");
            password=properties.getProperty("password");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public void getConnection(){
        try {
            Class.forName(Driver);
            connection = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public ResultSet selectSql(String sql){
        try {
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();

        } catch (Exception e) {
            throw new RuntimeException(e);
        }


        return resultSet;
    }


     public boolean findSql(String name){
        getConnection();
        try {
            String sql="select * from shop where name='"+name+"'";
            PreparedStatement preparedStatement1 = connection.prepareStatement(sql);
            ResultSet resultSet1 = preparedStatement1.executeQuery();
            while (resultSet1.next()){
                return true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return false;

    }
    public int addSql(Shop shop){
        getConnection();
        String sql="INSERT INTO shop(name,in_price,price,count,status)VALUES(?,?,?,?,?)";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setObject(1,shop.getName());
            preparedStatement.setObject(2,shop.getIn_price());
            preparedStatement.setObject(3,shop.getPrice());
            preparedStatement.setObject(4,shop.getCount());
            preparedStatement.setObject(5,shop.getStatus());
            int i = preparedStatement.executeUpdate();
            return i;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
    public int updataSql(Shop shop){
        getConnection();
        String sql="UPDATE shop set name=?,in_price=?,price=?,count=?,status=? where name=?";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setObject(1,shop.getName());
            preparedStatement.setObject(2,shop.getIn_price());
            preparedStatement.setObject(3,shop.getPrice());
            preparedStatement.setObject(4,shop.getCount());
            preparedStatement.setObject(5,shop.getStatus());
            preparedStatement.setObject(6,shop.getName());
            int i = preparedStatement.executeUpdate();
            return i;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }














    public void closeConnection(){
        try {
            if (resultSet!=null){
                resultSet.close();
                preparedStatement.close();
                connection.close();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}
